import jakarta.servlet.*;
import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;

/**
 * @author 张轩豪
 * @Date 2025/3/23:9:12
 * @Description:
 */
@WebFilter({"/stu","/student.jsp"})
public class LoginFilter implements Filter {
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException{
        HttpServletRequest req=(HttpServletRequest) servletRequest;
        HttpSession session=req.getSession();
        String username =(String) session.getAttribute("username");
        if (username!=null){
            filterChain.doFilter(servletRequest,servletResponse);
        }else {
            servletRequest.setAttribute("msg","请先登录");
            servletRequest.getRequestDispatcher("Login.jsp").forward(servletRequest,servletResponse);
        }
    }
}
